<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人脸识别应用</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body { font-family: Arial, sans-serif; }
        .container { max-width: 600px; margin: 20px auto; }
        .result-container { margin-top: 20px; }
        img { max-width: 100%; height: auto; }
        .face-item { margin-bottom: 10px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>人脸识别应用</h1>

        <h2>上传人脸图片</h2>
        <form id="uploadFaceForm">
            <input type="file" name="face_image" accept="image/*" required>
            <input type="text" name="name" placeholder="请输入姓名" required>
            <button type="submit">上传人脸</button>
        </form>

        <h2>已上传人脸</h2>
        <div id="faceList">
            {% for face_id, face_info in faces.items() %}
                <div class="face-item">
                    <span>{{ face_info.name }} (ID: {{ face_id }})</span>
                    <input type="text" placeholder="新姓名" class="new-name" data-id="{{ face_id }}">
                    <button class="edit-face" data-id="{{ face_id }}">编辑</button>
                    <button class="delete-face" data-id="{{ face_id }}">删除</button>
                </div>
            {% endfor %}
        </div>

        <h2>对比人脸图片</h2>
        <form id="compareFaceForm">
            <input type="file" name="compare_image" accept="image/*" required>
            <button type="submit">对比人脸</button>
        </form>

        <div class="result-container" id="resultContainer"></div>
        <div id="errorMessage" style="color: red;"></div>
    </div>

    <script>
        $(document).ready(function() {
            // 上传人脸图片
            $('#uploadFaceForm').submit(function(e) {
                e.preventDefault();
                var formData = new FormData(this);
                $.ajax({
                    url: '/upload_face',
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(response) {
                        location.reload(); // 刷新页面以显示新上传的人脸
                    },
                    error: function(xhr) {
                        $('#errorMessage').text(xhr.responseJSON.message);
                    }
                });
            });

            // 编辑人脸信息
            $(document).on('click', '.edit-face', function() {
                var face_id = $(this).data('id');
                var new_name = $(this).siblings('.new-name').val();
                $.ajax({
                    url: '/edit_face',
                    type: 'POST',
                    data: { face_id: face_id, new_name: new_name },
                    success: function(response) {
                        location.reload(); // 刷新页面以显示更新
                    },
                    error: function(xhr) {
                        $('#errorMessage').text(xhr.responseJSON.message);
                    }
                });
            });

            // 删除人脸信息
            $(document).on('click', '.delete-face', function() {
                var face_id = $(this).data('id');
                $.ajax({
                    url: '/delete_face',
                    type: 'POST',
                    data: { face_id: face_id },
                    success: function(response) {
                        location.reload(); // 刷新页面以显示更新
                    },
                    error: function(xhr) {
                        $('#errorMessage').text(xhr.responseJSON.message);
                    }
                });
            });

            // 对比人脸图片
            $('#compareFaceForm').submit(function(e) {
                e.preventDefault();
                var formData = new FormData(this);
                $.ajax({
                    url: '/compare',
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(response) {
                        $('#resultContainer').empty(); // 清空结果容器
                        var result_image = $('<img>').attr('src', '/results/' + response.result_image);
                        $('#resultContainer').append(result_image);
                        response.results.forEach(function(result) {
                            var info = $('<div>').text(result.name + ' (ID: ' + result.id + ')');
                            $('#resultContainer').append(info);
                        });
                    },
                    error: function(xhr) {
                        $('#errorMessage').text(xhr.responseJSON.message);
                    }
                });
            });
        });
    </script>
</body>
</html>
